package com.daocao.jy.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;


import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import com.daocao.jy.R;
import com.daocao.jy.base.BaseActivity;
import com.daocao.jy.util.UI;
import com.daocao.jy.view.V19FrameLayout;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class WebViewActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.webview)
    FrameLayout mWebContainer;


    private Unbinder unbiner;
    private WebView mWebView;
    private String title;
    private String description;
    private String shareUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        initWebViewBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        title = getIntent().getStringExtra("title");
        description = getIntent().getStringExtra("description");
        String id = getIntent().getStringExtra("id");
        String keyword = getIntent().getStringExtra("keyword");
        String url = getIntent().getStringExtra("url");
        mWebView = new WebView(getApplicationContext());
        mWebContainer.addView(mWebView);
//        getProtocol(keyword);
        if(TextUtils.isEmpty(title)){
            txtTitle.setText("");
        }else {
            txtTitle.setText(title);
        }

        setWebView(url);
    }


    private void setWebView(String content) {
        mWebContainer.setVisibility(View.VISIBLE);
        mWebView.setBackgroundColor(0);
//        mWebView.setWebChromeClient(new WebChromeClient());
        // // 设置可以支持缩放
        mWebView.getSettings().setSupportZoom(true);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setDefaultTextEncodingName("utf-8");
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                loadingDialog.show();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                loadingDialog.dismiss();
            }
        });

        mWebView.loadUrl(content);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mWebContainer.removeAllViews();
        if (mWebView != null) {
            mWebView.onPause();
            mWebView.destroy();
            mWebView = null;
        }
        unbiner.unbind();
        unbiner = null;
    }

    @Override
    public void onBackPressed() {

        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            finish();
        }
    }


    @OnClick({R.id.btn_back})
    public void onViewClicked(View view) {
        if (UI.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
        }
    }
}
